Pyramid Of Doom (programming)
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, the pyramid of doom is a common problem that arises when a program uses many levels of nested indentation to control access to a function. It is commonly seen when checking for
null pointer In computing, a null pointer or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown lengt ...
s or handling callbacks. Two examples of the term are related to a particular programming style in
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
, and the nesting of if statements that occurs in
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
languages when one of the objects may be a null pointer.


Examples

Most modern
object-oriented programming language Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
s use a coding style known as dot notation that allows multiple method calls to be written in a single line of code, each call separated by a period. For instance: theWidth = windows("Main").views(5).size().width(); This code contains four different instructions; it first looks in the collection of windows for a window with the name "Main", then looks in that window's views collection for the 5th subview within it, then calls the size method to return a structure with the view's dimensions, and finally calls the width method on that structure to produce a result that is assigned to a variable name theWidth. The problem with this approach is that the code assumes that all of these values exist. While it is reasonable to expect that a window will have a size and that size will have a width, it is not at all reasonable to assume that a window named "Main" will exist, nor that it has five subviews. If either of those assumptions is wrong, one of the methods will be invoked on null, producing a null pointer error. To avoid this error, the programmer has to check every method call to ensure it returns a value. A safer version of the same code would be: if windows.contains("Main") If the programmer wishes to use that value based on whether or not it exists and is valid, the functional code inside the if statements is all pushed to the right, making it difficult to read longer lines. This often leads to attempts to "flatten" the code: if windows.contains("Main") if theWindow != null && theWindow.views.contains(5) if theView != null Or alternatively: if !windows.contains("Main") else if !windows("Main").views.contains(5) else This sort of programming construct is very common and a number of programming languages have added some sort of
syntactic sugar In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
to address this. For instance, Apple's
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIFT, ...
added the concept of optional chaining in if statements while Microsoft's C# 6.0 and
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic (cl ...
14 added the null-conditional operators ?. and ? would_assign_null_to_theWidth_if_either_"Main"_or_the_fifth_subview_is_missing,_or_complete_the_statement_and_return_the_width_if_they_are_both_valid._There_are_many_times_where_the_programmer_wants_to_take_different_actions_in_these_two_cases,_so_Swift_adds_another_form_of_syntactic_sugar_for_this_role,_the_if_let_statement,_also_known_as_"optional_binding": if_let_theView_=_windows("Main")?.views(5)_


_Resolution

Pyramid_of_Doom_can_usually_be_resolved_in_any_language_by_simply_breaking_up_the_code_into_multiple_nested_functions_(or_other_groupings).__For_instance,_instead_of: main()_ You_can_break_up_the_functionality_like_this: doSomething()_ CC()_ main()_ Similarly,_data_structures_can_be_broken_up_by_levels,_when_similar_pyramids_occur. Not_only_is_the_Pyramid_of_Doom_solved,_but_it's_better_practice_to_not_have_large,_complicated_functions;_ smaller_ones_are_easier_to_get_right,_and_easier_to_read,_and_verify_the_operation_of. Choosing_function_names_for_each_of_these_levels_will_also_help_the_author_clarify_to_readers_what_is_done_where. Typically,_each_level_doesn't_need_many_connections_to_levels_that_are_far_away,_so_separating_them_out_is_easy. If_there_are_such_connections,_the_author_can_re-think_their_design_to_something_more_reliable,_because_this_is_a_fertile_source_of_bugs.


_See_also

*_Futures_and_promises.html" ;"title="/code> for member access and indexing, respectively. The basic idea is to allow a string of method calls to immediately return null if any of its members is null, so for instance: theWidth = windows("Main")?.views(5)?.size.width; would assign null to theWidth if either "Main" or the fifth subview is missing, or complete the statement and return the width if they are both valid. There are many times where the programmer wants to take different actions in these two cases, so Swift adds another form of syntactic sugar for this role, the if let statement, also known as "optional binding": if let theView = windows("Main")?.views(5)


Resolution

Pyramid of Doom can usually be resolved in any language by simply breaking up the code into multiple nested functions (or other groupings). For instance, instead of: main() You can break up the functionality like this: doSomething() CC() main() Similarly, data structures can be broken up by levels, when similar pyramids occur. Not only is the Pyramid of Doom solved, but it's better practice to not have large, complicated functions; smaller ones are easier to get right, and easier to read, and verify the operation of. Choosing function names for each of these levels will also help the author clarify to readers what is done where. Typically, each level doesn't need many connections to levels that are far away, so separating them out is easy. If there are such connections, the author can re-think their design to something more reliable, because this is a fertile source of bugs.


See also

* Futures and promises">Promises
, a technique for avoiding the pyramid of doom, e.g. used in JavaScript *Law of Demeter * Safe navigation operator, a programming language operator that lets one avoid the pyramid of doom


References

{{reflist Programming constructs]